define(
	['jquery',
	'dialog',
	'underscore',
	'backbone',
	'fileuploader',
	'catjs/util',
	'user',
	'minimap',
	'map',
	'catjs/String',
	],
	function($,dialog,_,Backbone,uploader,util,user,mini,map,str){
	var module = {};

	//个人设置视图
	var SettingView = Backbone.View.extend({

		el : $('#dialog1 .dialog1-bd'),
		template : $('#tplSetting').text(),
		//成功状态
		successState : '<div class="icon_update_success"></div>',
		//失败状态
		failState : '<div class="icon_update_fail"></div>',
		events : {
			'change .btn-set-nickname' : 'setNickname',
			'change .btn-set-realname' : 'setRealname',
			'change .btn-set-birthday' : 'setBirthday',
			'change .btn-set-home' : 'setHome',
			'change .btn-set-school' : 'setSchool',
			'change .btn-set-studentId' : 'setStudentId',
			'change .btn-set-dorm' : 'setDormId',
			'click .btn-set-psw' : 'setPsw',
		},

		render : function(){
			var self = this;
			//获取宿舍列表
			$.ajax({
				'url' : '/Node/GetAllDorms',
				success : function(json){
					var context = {};
					context = util.getUser();
					context.dorms = JSON.parse(json);
					self.$el.html( util.ejs.render(self.template,context) );
					//头像上传
					self.uploadAvatar();
					//显示对话框
					self.showDlg();
				},	
			});
		},

		showDlg : function(){
			$('#dialog1 .dialog1-wrap').css({
				'width' : 720,
			});
			$('#dialog1').fadeIn();
			$('#dialog1').fadeIn();
		},
		uploadAvatar : function(){
			var self = this;
			//监听头像上传
			var user = util.getUser();
			new uploader.FileUploader({
				element: self.$el.find('.btn-set-avatar')[0],
				action: '/User/UpdateAvatar',
				allowedExtensions: ['jpg', 'jpeg', 'png', 'gif'],
				params : {
					userId : user.userId,
					realName : user.userInfo.realName,
					studentId : user.userInfo.studentId,
					school : user.userInfo.school,
					birthday : user.userInfo.birthday,
					homeAddress : user.userInfo.homeAddress,
					dormId : user.userInfo.dormId,
					nickName : user.nickName,
				},
				debug: true,
				onComplete : function(id,fileName,json){
					//保存至cookie
					user.userInfo.avatar = json.avatar;
					util.setUser(user);
					//刷新头像
					self.$el.find('.user_pic_big').attr('src','/static/images/'+json.avatar);
				},
			});
		},
		//修改昵称
		setNickname : function(){
			var self = this;
			var user = util.getUser();
			user.nickName =
				self.$el.find('input[name=nickName]').val();
			self.update(
				user,
				$('input[name=nickName]').next()
			);
		},
		//修改姓名
		setRealname : function(){
			var self = this;
			var user = util.getUser();
			user.userInfo.realName =
				self.$el.find('input[name=realName]').val();
			self.update(
				user,
				$('input[name=realName]').next()
			);
		},
		//修改生日
		setBirthday : function(){
			var self = this;
			var user = util.getUser();
			user.userInfo.birthday =
				self.$el.find('input[name=birthday]').val();
			self.update(
				user,
				$('input[name=birthday]').next()
			);
		},
		//修改住址
		setHome : function(){
			var self = this;
			var user = util.getUser();
			user.userInfo.homeAddress =
				self.$el.find('input[name=homeAddress]').val();
			self.update(
				user,
				$('input[name=homeAddress]').next()
			);
		},
		//修改学校
		setSchool : function(){
			var self = this;
			var user = util.getUser();
			user.userInfo.school =
				self.$el.find('input[name=school]').val();
			self.update(
				user,
				$('input[name=school]').next()
			);
		},
		//修改学号
		setStudentId : function(){
			var self = this;
			var user = util.getUser();
			user.userInfo.studentId =
				self.$el.find('input[name=studentId]').val();
			self.update(
				user,
				$('input[name=studentId]').next()
			);
		},
		//修改寝室
		setDormId : function(e){
			var self = this;
			var user = util.getUser();
			user.userInfo.dormId = e.currentTarget.selectedOptions[0].value;
			self.update(
				user,
				$(e.currentTarget).next()
			);
		},
		//修改密码
		setPsw : function(){
			var self = this;
			var oldPsw , newPsw , userId;
			$.ajax({
				url : '/User/UpdatePassword',
				type : 'POST',
				data : {
					oldPsw : self.$el.find('input[name=oldPsw]').val(),
					newPsw : self.$el.find('input[name=newPsw]').val(),
					userId : util.getUser().userId, 
				},
				success : function(){
					$('.psw_box').find('.msg_update_state').html(self.successState);
					//清空输入框
					self.$el.find('input[name=oldPsw]').val('');
					self.$el.find('input[name=newPsw]').val('');
				},
				error : function(){
					$('.psw_box').find('.msg_update_state').html(self.failState);
					//清空输入框
					self.$el.find('input[name=oldPsw]').val('');
					self.$el.find('input[name=newPsw]').val('');
				},
			});
		},
		//更新
		update : function(user,state){
			var self = this;
			//更新cookie
			util.setUser(user);
			$.ajax({
				url : '/User/UpdateUser',
				type : 'POST',
				data : {
					userId : user.userId,
					realName : user.userInfo.realName,
					studentId : user.userInfo.studentId,
					school : user.userInfo.school,
					birthday : user.userInfo.birthday,
					homeAddress : user.userInfo.homeAddress,
					avatar : user.userInfo.avatar,
					dormId : user.userInfo.dormId,
					nickName : user.nickName,
				},
				//成功更新
				success : function(){
					state.html( self.successState );
				},
				//更新失败
				error : function(){
					state.html( self.failState );
				},
			});
		},
	});
	var setting = new SettingView;

	//迷你地图视图
	var MinimapView = Backbone.View.extend({

		el : $('#dialog1 .dialog1-bd'),
		template : $('#tplMinimap').text(),
		events : {
			'click .minimap-landmark' : 'landmark',
		},

		//渲染前的初始化操作
		init : function(){
			$('#dialog1 .dialog1-wrap').css({
				width : 1000,
			});
		},
		render : function(){
			var self = this;
			self.init();
			//加载地图
			$('#dialog1 .dialog1-bd').html( $('#tplMinimap').text() );
			//设置当前位置
			var current_node = map.current_node;
			var NODE_HOR_NUM = map.NODE_HOR_NUM;
			var x = current_node % NODE_HOR_NUM;
			var y = Math.round(current_node / NODE_HOR_NUM);
			$('#dialog1 .minimap-current').css({
				'top' : y * 60,
				'left' : x * 150,
			});

			$('#dialog1').fadeIn();
			$('#shadow1').fadeIn();
			mini.init();
		},
		landmark : function(e){
				$('#dialog4').css({
					'top' : 55,
				});
				$('#dialog4 .dialog1-wrap').css({
					width : 700
				});
				$('#dialog4 .dialog1-bd').css({
					'background' : 'url(/static/img/{0}.jpg)'.format(e.toElement.className.split(' ')[1]),
					'background-size' : '700px 500px',
					'width' : 700,
					'height' : 500,
					'margin' : 0,
					'padding' : 0,
				});
				$('#dialog4').fadeIn();	
				$('#shadow4').fadeIn();
		},
	});
	var minimap = new MinimapView;

	module.init = function(){
		$('.function').css('display','block');
		//个人设置
		$('.function .setting').click(function(){
			setting.render();
		});
		//注销
		$('.function .logout').click(function(){
			//删除cookie并转跳
			util.eraseCookie('user');
			window.location = '/';
		});
		//迷你地图
		$('.search').click(function(){
			minimap.render();
		});
	};


	return module;
});